package com.lanou.activity.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.activity.Service.DiscountService;
import com.lanou.activity.utils.ToolUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/act")
public class DiscountController {

    @Resource
    private DiscountService discountService;


    /**
     * @param
     * @description 访问优惠券添加页面
     * @author 66
     * @date 17/9/23
     */
    @RequestMapping("/add")
    @ResponseBody
    public ModelAndView addDiscount() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("addDiscount");
        return modelAndView;
    }

    /**
     * @param
     * @description 根据  优惠表中优惠类型con_type从促销条件表中查询对应的 促销条件名称con_name
     * @author 66
     * @date 17/9/23
     */
    @RequestMapping("selectConNoByConType")
    @ResponseBody
    public List<Map<String, Object>> selectConNoByConType(@RequestParam String conType) {
        List<Map<String, Object>> list = discountService.selectConnameByContype(conType);
        System.out.println("list=" + list.toString());
        return list;
    }

    /**
     * @param
     * @description 获取表单数据 执行添加优惠的操作
     * @author 66
     * @date 17/9/23
     */
    @RequestMapping("/addDiscountInfo")
    @ResponseBody
    public Map<String, Object> addDiscountInfo(@RequestParam Map<String, Object> params) {
        String value = ToolUtil.AccountGenerator("dis");
        params.put("disNo", value);
        discountService.addDiscountInfo(params);
        params.put("result", "success");
        return params;
    }

    /**
     * @param
     * @description 访问优惠管理页面
     * @author 66
     * @date 17/9/26
     */
    @RequestMapping("manage")
    @ResponseBody
    public ModelAndView manageDiscount(@RequestParam Map<String, Object> params) {
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<Map<String, Object>> pageInfo = discountService.selectDiscountByPageList(params);
        modelAndView.addObject("params", params);
        modelAndView.addObject("page", pageInfo);
        modelAndView.addObject("list", pageInfo.getList());
        modelAndView.setViewName("manageDiscount");
        return modelAndView;
    }


    /**
     * @description  根据不同的优惠信息编码  查询数据回显
     * @author 66
     * @date 17/9/26
     * @param
     */
    @RequestMapping("selectByDisno")
     @ResponseBody
    public  ModelAndView  selectByDisno(String disNo){
        ModelAndView modelAndView=new ModelAndView();
        List<Map<String, Object>> list = discountService.selectByDisno(disNo);
        Map<String, Object> result=list.get(0);
        result.put("disNo",disNo);
        modelAndView.addObject("result",result);
        modelAndView.setViewName("updateDiscount");
        return modelAndView;
    }

    /**
     * @description  更新优惠信息
     * @author 66
     * @date 17/9/26
     * @param
     */
    @RequestMapping("updateDis")
    @ResponseBody
     public Map<String,Object>  updateDis(Map<String,Object> params){
         Map map=new HashMap();
         System.out.println(params.toString());
          discountService.updateDis(params);
          map.put("result","success");
          return  map;
     }

}
